package top.hekun.study.core;

/**
 * 〈helloWord 启动测试〉
 *
 * @author: Sakura
 * @date: 2023/4/23
 */
public class HelloWordRun {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        // 调用类加载器
        MyClassLoader myClassLoader = new MyClassLoader();
        // 获取类
        Class<?> loadClass = myClassLoader.loadClass("top.hekun.study.core.model.HelloWord");
        // 实例化
        Object newInstance = loadClass.newInstance();
        // 打印 会自动调用 toString()
        System.out.println(newInstance);
    }

    public static class MyClassLoader extends ClassLoader {
        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            if ("top.hekun.study.core.model.HelloWord".equals(name)) {
                byte[] classByte = HelloWordDumo.dump();
                Class<?> aClass = defineClass(name, classByte, 0, classByte.length);
                return aClass;
            }
            throw new ClassNotFoundException("找不到类 : " + name);
        }
    }

}
